<?php

namespace Index\Controller;


use Think\Controller;

class LoginController extends Controller {
	public function index() {
		$this->display('login');
	}
	
	public function logout(){
		session_unset();
		session_destroy();
		$this->redirect('/Index/Login/index');
	}
	
	public function login() {
		
		$db = M ( 'user' );
		$user = $db->where ( array (
				'username' => I('username'),
				'password' => md5(I('password')) 
		) )->find ();
		//echo $db->_sql ();
		$loginnum = $user['loginnum'];
		
		if(!$user || $user['password'] != I('password', '', 'md5')){
			$this->error('帐号或密码错误！');
			return;
		}
		//更新登录信息
		$data = array(
				'id' => $user['id'],
				'logintime' => date('Y-m-d H:i:s'),
				'loginip' => get_client_ip(),
				'loginnum' => $loginnum + 1,
		);
		$db->save($data);
		
		//获取最新登录信息
		$user1 = $db->where ( array (
				'username' => I('username'),
				'password' => md5(I('password'))
		) )->find ();
		//存储为session
		session('uid', $user1['id']);
		session('username', $user1['username']);
		session('logintime', $user1['logintime']);
		session('loginip', $user1['loginip']);
		session('loginnum', $user1['loginnum']);
		session('regtime', $user1['regtime']);
		session('guid', $user1['guid']);

		//$this->logs();
		
		if($user){
			//$this->success('登录成功！','loginTo');
// 			echo session('logintime');
// 			echo date('Y-m-d H:i:s');
// 			echo time();
			redirect(__APP__.'/Index/index');
			//redirect(__APP__.'/Public/header');
		}else{
			$this->error('登录失败！');
		}
		
		//addOperationLog();
	}
}